package com.jicheng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jicheng.Utils.MD5Util;
import com.jicheng.Utils.ResultMess;
import com.jicheng.dto.logs.LogsDetailDto;
import com.jicheng.dto.sysUser.*;
import com.jicheng.entity.SysRole;
import com.jicheng.entity.SysUser;
import com.jicheng.ext.dto.PagingResult;
import com.jicheng.ext.dto.SearchPagingParams;
import com.jicheng.mapper.SysUserMapper;
import com.jicheng.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Permission;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: SysUserController
 * @Description: TODO
 * @author: khz
 * @date: 2020年08月26日 11:25
 */
@Api(tags = "sys-user(系统用户管理)")
@RestController("sys-user")
public class SysUserController {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserService sysUserService;

    @PostMapping("/addUser")
    @ApiOperation(value = "添加系统用户")
    public ResultMess addUser(@RequestBody SysUserParams sysUser) {
        //将uuid设置为密码盐值
        sysUser.setPassword(MD5Util.shiroEncryption(sysUser.getPassword()));
        try {
            sysUserService.addUser(sysUser);
            return new ResultMess(true, "添加成功！");

        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "添加失败！");
        }
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码")
    public ResultMess updatePassword(@RequestBody UpdatepassParams updatepassParams) {
        //加密后的密码
        String encryption = MD5Util.shiroEncryption(updatepassParams.getNewPassword());
        try {
            sysUserService.updatePassword(updatepassParams.getUserId(), encryption);
            return new ResultMess(true, "更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "更新失败！");
        }
    }

    @PostMapping("/updateRole")
    @ApiOperation(value = "修改角色")
    public ResultMess updateRole(@RequestBody UserRoleParams params) {

        try {
            sysUserService.updateRole(params.getUserId(), params.getRoleId());
            return new ResultMess(true, "更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "更新失败！");
        }
    }

    @GetMapping("/userList")
    @ApiOperation(value = "系统用户角色列表")
    public PagingResult<SysUserDto> userList(SearchPagingParams searchPagingParams) {
        PageHelper.startPage(searchPagingParams.getPage(), searchPagingParams.getPageSize());

        List<SysUserDto> list = sysUserService.userList(searchPagingParams);
        PageInfo pageInfo = new PageInfo<>(list);
        PagingResult<SysUserDto> result = new PagingResult();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;

    }

    @PostMapping("/addRole")
    @ApiOperation(value = "添加角色")
    public ResultMess addRole(@RequestBody SysRoleParams sysRole) {
        sysUserService.addRole(sysRole);
        return new ResultMess(true, "添加成功！");
    }

    @PostMapping("/addPermission")
    @ApiOperation(value = "添加权限")
    public ResultMess addPermission(@RequestBody SysPermiParams sysPermiParams) {
        sysUserService.addPermission(sysPermiParams);
        return null;
    }

    @GetMapping("/getRoleList")
    @ApiOperation(value = "获取角色下拉")
    public ResultMess<RoleDto> getRoleList() {
        List<RoleDto> list = sysUserService.getRoleList();
        return new ResultMess(true, "", list);
    }

    @PostMapping("/setPermisson")
    @ApiOperation(value = "设置权限")
    public ResultMess setPermisson(@RequestBody RolePermiParams rolePermiParams) {
        try {
            sysUserService.setPermisson(rolePermiParams);
            return new ResultMess(true, "设置成功！");

        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "设置失败！");
        }
    }

    @GetMapping("/getRolePermission")
    @ApiOperation(value = "获取角色权限列表")
    public PagingResult<RolePermiDto> getRolePermission(SearchPagingParams searchPagingParams) {
        PageHelper.startPage(searchPagingParams.getPage(), searchPagingParams.getPageSize());

        List<RolePermiDto> list = sysUserService.getRolePermission(searchPagingParams);
        PageInfo pageInfo = new PageInfo<>(list);
        PagingResult<RolePermiDto> result = new PagingResult();
        result.setList(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @GetMapping("/getPermiforRole")
    @ApiOperation(value = "获取角色对应的权限")
    public ResultMess<PermiforRoleDto> getPermiforRole(String roleId) {
        List<PermiforRoleDto> list = sysUserService.getPermiforRole(roleId);
        return new ResultMess(true, "", list);
    }

    @GetMapping("/delUser")
    @ApiOperation(value = "删除用户")
    public ResultMess delUser(int userId) {
        try {
            //删除用户
            sysUserService.delUser(userId);
            return new ResultMess(true, "删除成功！", null);
        } catch (Exception e) {
            return new ResultMess(false, "删除失败！" + e.getMessage(), null);
        }
    }

    @GetMapping("/resetPass")
    @ApiOperation(value = "重置密码")
    public ResultMess resetPass(String userId) {
        try {
            sysUserService.resetPass(userId);
            return new ResultMess(true, "密码重置成功！", null);
        } catch (Exception e) {
            return new ResultMess(false, "密码重置失败！" + e.getMessage(), null);
        }
    }

    @GetMapping("/delRole")
    @ApiOperation(value = "删除角色")
    public ResultMess delRole(@RequestParam("roleId") int roleId) {
        try {
            //删除角色
            sysUserService.delRole(roleId);
            return new ResultMess(true, "删除成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMess(false, "删除失败！" + e.getMessage(), null);
        }
    }
}
